import { ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'

export default function useLanguage() {
  const { locale } = useI18n()
  const currentLanguage = ref(localStorage.getItem('language') || 'zh')

  // 初始化语言
  locale.value = currentLanguage.value

  // 切换语言
  const toggleLanguage = () => {
    currentLanguage.value = currentLanguage.value === 'zh' ? 'en' : 'zh'
    locale.value = currentLanguage.value
    localStorage.setItem('language', currentLanguage.value)
  }

  // 监听语言变化
  watch(currentLanguage, (newLang) => {
    document.documentElement.setAttribute('lang', newLang)
  })

  return {
    currentLanguage,
    toggleLanguage
  }
}